package com.lovo.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.stock.bean.PageInfo;
import com.lovo.stock.bean.SadetailBean;
import com.lovo.stock.bean.SaleBean;
import com.lovo.stock.mapper.SaleMapper;
import com.lovo.stock.service.ISadetailService;
import com.lovo.stock.service.ISaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SaleServiceImpl extends ServiceImpl<SaleMapper, SaleBean> implements ISaleService {
    @Autowired
    SaleMapper saleMapper;

    @Autowired
    ISadetailService sadetailService;

    @Override
    public IPage<SaleBean> querySaleByPage(PageInfo<SaleBean> pageInfo) {
        //客户名称，起始日期，结束日期
        QueryWrapper q=new QueryWrapper<>();
        if (pageInfo.getStartTime()!=null&&!"".equals(pageInfo.getStartTime())){
            q.ge("sale_date",pageInfo.getStartTime());
        }
        if (pageInfo.getEndTime()!=null&&!"".equals(pageInfo.getEndTime())){
            q.le("sale_date",pageInfo.getEndTime());
        }
        //判断信息对象存在
        if (pageInfo.getData()!=null){
            //存在，则判断用户名是否存在
            if (pageInfo.getData().getSaleCustomerName()!=null&&!"".equals(pageInfo.getData().getSaleCustomerName())){
                q.like("sale_customer_name",pageInfo.getData().getSaleCustomerName());
            }
        }

        IPage<SaleBean> page =saleMapper.selectPage(new Page<>(pageInfo.getCurPage(),pageInfo.getPageSize()),q);
        return page;
    }

    @Override
    public SaleBean querySaleById(int id) {
        //首先查询销售信息
        SaleBean saleBean=saleMapper.selectById(id);
        //再查询销售明细表的信息
        List<SadetailBean> sadetailBeans=sadetailService.querySaDetailBySaleId(id);
        //将销售明细填入销售信息
        saleBean.setSadetailBeans(sadetailBeans);
        return saleBean;
    }

    @Override
    public int addSale(SaleBean saleBean) {
        saleMapper.insert(saleBean);
        return saleBean.getSaleId();
    }
}
